package cn.nami.web.frontpage.jdo.core;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import cn.nami.web.db.util.PMF;
import cn.nami.web.util.StringUtil;

public class CommonJdo
{
	private CommonJdo(){};

	private static final Logger logger = Logger.getLogger(CommonJdo.class.getName());
	
	/**
	 * persistent a object to data store.
	 */
	public static <T> void save(T obj)
	{
		logger.info("Persistent object begin...");
		if(obj == null)
		{
			logger.warning("Persistent object failure : the object is null.");
			return;
		}
		PersistenceManager pm = null;
		try
		{
			pm = PMF.get().getPersistenceManager();
			pm.makePersistent(obj);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			logger.severe("Persistent a object error, object : " + obj + e.getMessage());
		}
		finally
		{
			PMF.close(pm);
			logger.info("Persistent object end...");
		}
	}
	
	/**
	 * delete a object
	 */
	public static <T> void deleteById(Class<T> clazz, Integer id)
	{
		logger.info("delete object begin...");
		PersistenceManager pm = null;
		try
		{
			pm = PMF.get().getPersistenceManager();
			T obj = pm.getObjectById(clazz, id);
			pm.deletePersistent(obj);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			logger.severe("delete object error : " + e.getMessage());
		}
		finally
		{
			PMF.close(pm);
			logger.info("delete object end...");
		}
	}
	
	/**
	 * get an object by id
	 */
	public static <T> T get(Class<T> clazz, Integer id)
	{
		if(id == null)
		{
			return null;
		}
		logger.info("delete object begin...");
		PersistenceManager pm = null;
		T obj = null;
		try
		{
			pm = PMF.get().getPersistenceManager();
			obj = pm.getObjectById(clazz, id);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			logger.severe("delete object error : " + e.getMessage());
		}
		finally
		{
			PMF.close(pm);
			logger.info("delete object end...");
		}
		return obj;
	}

	/**
	 * count the list size
	 */
	public static <T> Integer count(Class<T> clazz)
	{
		logger.info("count object begin...");
		PersistenceManager pm = null;
		Integer count = 0;
		try
		{
			pm = PMF.get().getPersistenceManager();
			count = (Integer)pm.newQuery("select count(1) from " + clazz.getName()).execute();
		}
		catch (Exception e)
		{
			e.printStackTrace();
			logger.severe("count object error : " + e.getMessage());
		}
		finally
		{
			PMF.close(pm);
		}
		return count;
	}
	
	/**
	 * list object from db
	 */
	@SuppressWarnings("unchecked")
	public static <T> List<T> listAll(Class<T> clazz, Integer begin, Integer pageSize, String order)
	{
		return list(clazz, null, begin, pageSize, order);
	}
	
	/**
	 * list by condition
	 */
	public static <T> List<T> list(Class<T> clazz, List<String> condition)
	{
		return list(clazz, condition, -1, -1, null);
	}
	
	/**
	 * list by condition
	 */
	public static <T> List<T> list(Class<T> clazz, String condition)
	{
		List<String> param = null;
		if(StringUtil.isNotEmpty(condition))
		{
			param = new ArrayList<String>();
			param.add(condition);
		}
		return list(clazz, param, -1, -1, null);
	}
	
	/**
	 * list by condition
	 */
	@SuppressWarnings("unchecked")
	public static <T> List<T> list(Class<T> clazz, List<String> condition, Integer begin, Integer pageSize, String order)
	{
		logger.info("List all object begin...");
		List<T> list = null;
		String sql = "select from " + clazz.getName();
		if(StringUtil.isNotEmpty(order))
		{
			sql = sql + " order by " + order;
		}
		PersistenceManager pm = null;
		try
		{
			pm = PMF.get().getPersistenceManager();
			
			String queryParam = getQueryParam(condition);
			Query query = null;
			if(StringUtil.isEmpty(queryParam))
			{
				query = pm.newQuery(sql);
			}
			else
			{
				query = pm.newQuery(sql + " where " + queryParam);
			}
			if(begin >= 0)
			{
				query.setRange(begin, begin + pageSize);
			}
			list = (List<T>)query.execute();
		}
		catch (Exception e)
		{
			e.printStackTrace();
			logger.severe("list all object error : " + e.getMessage());
		}
		finally
		{
			PMF.close(pm);
		}
		return list;
	}

	private static String getQueryParam(List<String> condition)
	{
		if(condition == null || condition.size() <= 0)
		{
			return null;
		}
		StringBuffer buf = new StringBuffer(100);
		for(String tmp : condition)
		{
			if(StringUtil.isEmpty(tmp))
			{
				continue;
			}
			buf.append(" && ").append(tmp);
		}
		String rtn = buf.toString().replaceFirst("&&", "");
		logger.info("query param is : " + rtn);
		return rtn;
	}
}